# coding: utf-8
import webapp2
import jinja2

from settings import STATIC_PATH
import os
import core.search

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname("templetes/")))
    
class MainPage(webapp2.RequestHandler):
    def get(self):
        keyword = self.request.get('keyword')       # required
        area = self.request.get('area')             # not required, default *
        providerid = self.request.get('providerid')     # not required, default *
        start = self.request.get('start')           # not required, default 0
        limit = self.request.get('limit')           # not required, default 10
        expired = self.request.get('expired')       # not required, default 1
        action = self.request.get('action')
        
        # Check data
        if keyword == "":
            keyword = "*" 
        if area == "":
            area = "*" 
        if action == "":
            action = "exact"
            
        providerid = int(providerid) if providerid != "" else 10000
        start = int(start) if start != "" else 0
        limit = int(limit) if limit != "" else 50
        expired = int(expired) if expired != "" else 1
        
        if keyword == "*":            
            product_list = core.search.SearchProducts.exactSearch(keyword, area, providerid, start, limit, expired)
            related_keyword = ""
        else:
            product_list, related_keyword = core.search.SearchProducts.relatedSearch(keyword, area, providerid, start, limit, expired)
            related_keyword = ",".join(related_keyword)
        
        
        big_list = []
        med_list = []
        small_list= []
        
        cnt = 0;
        for product in product_list:
            product.title = product.title.replace("\"", "\\\"")
            if cnt <5:
                big_list.append(product)
            elif cnt < limit/2:
                med_list.append(product)
            else:
                small_list.append(product)
            cnt = cnt +1
             
        # for Html output
        self.response.headers['Content-Type'] = 'text/html'
        template = jinja_environment.get_template('today_tiled.html')
        self.response.out.write(template.render({     
                                                 "menu_name" : "today_tiled",                                           
                                                 "static_path" : STATIC_PATH,
                                                 "big_list" : big_list,
                                                 "med_list" : med_list,
                                                 "small_list" : small_list,
                                                 "items": product_list, 
                                                 "related_keyword": related_keyword,
                                                 "keyword": keyword, 
                                                 "area": area, 
                                                 "provider": providerid, 
                                                 "start": start, 
                                                 "expired": expired}))
        
    
app = webapp2.WSGIApplication([('/today_tiled', MainPage)], debug=True)